package com.javasm.adminapi.aftersales.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.adminapi.aftersales.service.RepairShopService;
import com.javasm.adminapi.module.aftersalesbase.entity.Repair;
import com.javasm.adminapi.module.aftersalesbase.entity.RepairType;
import com.javasm.adminapi.module.aftersalesbase.service.RepairService;
import com.javasm.adminapi.module.aftersalesbase.service.RepairTypeService;
import com.javasm.adminapi.module.aftersalesbase.vo.SearchVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @className: RepairServiceImpl
 * @description:
 * @author: zjl
 * @date: 2025/10/15 9:50
 * @version: 0.1
 * @since: jdk17
 */
@Service("repairShopService")
public class RepairShopServiceImpl implements RepairShopService {
    @Resource
    RepairService repairService;

    @Resource
    RepairTypeService repairTypeService;

    @Override
    public PageInfo<Repair> queryByCondition(SearchVo searchVo) {
        PageHelper.startPage(searchVo.getPageNum(),searchVo.getPageSize());
        LambdaQueryWrapper<Repair> queryWrapper = new LambdaQueryWrapper<>();
        if(searchVo.getName()!=null){
            queryWrapper.and(wrapper ->
                    wrapper.like(Repair::getRepName,searchVo.getName()));
        }
        if(searchVo.getPhone()!=null){
            queryWrapper.and(wrapper ->
                    wrapper.like(Repair::getRepNumber,searchVo.getPhone()));
        }
        if(searchVo.getType()!=null){
            queryWrapper.and(wrapper ->
                    wrapper.eq(Repair::getStatus,searchVo.getType()));
        }
        if(searchVo.getPro()!=null){
            queryWrapper.and(wrapper ->
                    wrapper.like(Repair::getRepPro,searchVo.getPro()));
        }
        if(searchVo.getCity()!=null){
            queryWrapper.and(wrapper ->
                    wrapper.like(Repair::getRepCity,searchVo.getCity()));
        }
        queryWrapper.orderByDesc(Repair::getId);
        return new PageInfo<>(repairService.list(queryWrapper));
    }

    @Override
    public void saveOrUpdate(Repair repair) {
        if (!StringUtils.isEmpty(repair.getId())){
            repair.setUpdateTime(new Date());
        }else {
            repair.setCreateTime(new Date());
        }
        repair.setStatus(1);
        repairService.saveOrUpdate(repair);
    }

    @Override
    public void removeByIds(Integer[] ids) {

        repairService.removeByIds(List.of(ids));
    }

    @Override
    public List<RepairType> proCityList() {
        List<RepairType> list = repairTypeService.list();
        List<RepairType> proList = list.stream().filter(repairType ->
                        repairType.getPid().equals("-1"))
                .collect(Collectors.toList());
        proList.forEach(pro -> {
            List<RepairType> cityList = list.stream().filter(city ->
                            city.getPid().equals(pro.getRepLabel()))
                    .collect(Collectors.toList());
            pro.setChildRep(cityList);
        });
        return proList;
    }
}
